package com.example.text.ui.login

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels

import androidx.navigation.NavController
import androidx.navigation.fragment.NavHostFragment
import com.example.text.R
import com.example.text.base.BaseToolBarActivity
import com.example.text.databinding.ActivityLoginBinding
import com.example.text.observeWithLifecycle
import com.example.text.ui.login.view.PhoneLoginFragment
import com.example.text.ui.login.viewmodel.LoginViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class LoginActivity : BaseToolBarActivity<LoginViewModel,ActivityLoginBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_login



    private val mViewModel:LoginViewModel by viewModels()

    private lateinit var mNavController: NavController //导航控制器

    override fun initView() {
        super.initView()//nav_host_container
        mNavController = (supportFragmentManager.findFragmentById(R.id.fragmentContainerView) as NavHostFragment).navController
    }
    override fun initListener() {
        //订阅正在显示fragment
        mViewModel.currentFragment.observeWithLifecycle(this){
            manageFragment(it)
        }
        //密码登陆
        mToolbar.setOnMenuItemClickListener {
            if (it.itemId == R.id.action) {
                mNavController.navigate(R.id.action_phoneLoginFragment_to_accountLoginFragment)
            }
            true
        }
    }

    private fun manageFragment(it: String) {
        title = ""
        mToolbar.menu.clear()
        when (it) {
            PhoneLoginFragment::class.simpleName -> {
                mToolbar.setNavigationIcon(R.mipmap.icon_exit)
                mToolbar.inflateMenu(R.menu.single_menu)
                mToolbar.menu.getItem(0).title = "密码登陆"
                mToolbar.menu.getItem(0).icon = null
            }
            else -> {
                mToolbar.setNavigationIcon(androidx.appcompat.R.drawable.abc_ic_ab_back_material)
            }

        }
    }




}